﻿<?php
class Compact_CompactTypeController extends AppController
{
    public function listAction()
    {
        $objCompactTypeTable = new Cts_Db_CompactType() ;
        $aParams = $this->getRequest()->getParams() ;
        $nStart = isset( $aParams['start'] ) ? $aParams['start'] : 0 ;
        $nLimit = isset( $aParams['limit'] ) ? $aParams['limit'] : 0 ;
        
        try
        {   
            $aCompactType = $objCompactTypeTable->getList( '1=1' , NULL , NULL ) ;
            $nCountCompactType = count( $aCompactType ) ;
            
            $aResult = array(  
                'datacount'  => $nCountCompactType ,
                'datas'      => $aCompactType
            ) ;
            
            $strResult = Zend_Json::encode( $aResult ) ;
            echo $strResult ;
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
        }
    }
    
    public function addAction()
    {
        $objCompactTypeTable = new Cts_Db_CompactType() ;
        $aParams = $this->getRequest()->getParams() ;
        $strNewType = trim( $aParams['strTypeName'] ) ;
        $strLocator = 'name = \'' . $strNewType . '\'' ;
        
        try
        {
            $aCompactType = $objCompactTypeTable->getList( $strLocator , NULL , NULL ) ;
            if( empty( $aCompactType ) )
            {
                $aInsertData = array(
                    'name'   =>   $strNewType 
                );        
                $nResult = $objCompactTypeTable->insert( $aInsertData ) ;
                if( $nResult > 0 )
                {
                    $strResult = '添加合同类型 '.$strNewType.' 成功' ;
                    Cts_Json::echoResult( COMPACT_TYPE_ADD_SUCCESSFUL , $strResult ) ;
                    //write operation into db
    		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;   
                }
                else
                {
                    $strResult = '添加合同类型 '.$strNewType.' 失败，请检查数据库连接。' ;
                    Cts_Json::echoResult( COMPACT_TYPE_ADD_FAILD , $strResult ) ;  
                    //write operation into db
    		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
                }
            }
            else
            {
                $strResult = $strNewType.'合同类型已经存在。请重新输入。' ;
                Cts_Json::echoResult( COMPACT_TYPE_IS_ALREADE_HAVE , $strResult ) ;
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }
    }
    
    public function updateAction()
    {
        $objCompactTypeTable = new Cts_Db_CompactType() ;
        $aParams = $this->getRequest()->getParams() ;
        $strNewType = trim( $aParams['strTypeName'] ) ;
        $nTypeId = intval( $aParams['nTypeId'] ) ;
        
        try
        {
            if( !empty( $strNewType ) && $nTypeId > 0 )
            {
                $aUpdateData = array(
                    'name'   =>   $strNewType 
                );        
                $strLocator = 'id = '.$nTypeId ;
                $nResult = $objCompactTypeTable->update( $aUpdateData , $strLocator ) ;
                if( $nResult > 0 )
                {
                    $strResult = '修改合同类型 '.$strNewType.' 成功' ;
                    Cts_Json::echoResult( COMPACT_TYPE_UPDATE_SUCCESSFUL , $strResult ) ;
                    //write operation into db
    		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;   
                }
                else
                {
                    $strResult = '修改合同类型 '.$strNewType.' 失败，请检查数据库连接。' ;
                    Cts_Json::echoResult( COMPACT_TYPE_UPDATE_FAILD , $strResult ) ;  
                    //write operation into db
    		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
                }
            }
            else
            {
                $strResult = '提交的参数不合法，请重新输入信息。' ;
                Cts_Json::echoResult( COMPACT_TYPE_PARAMS_INVALIDED , $strResult ) ;  
                //write operation into db
		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }    
    }
    
    public function deleteAction()
    {
        $objCompactTypeTable = new Cts_Db_CompactType() ;
        $objCompactTable = new Cts_Db_Compact() ;
        $aParams = $this->getRequest()->getParams() ;
        $nTypeId = intval( $aParams['nTypeId'] ) ;
        
        try
        {
            if( $nTypeId > 0 )
            {       
                $strLocator = 'type = '.$nTypeId ;
                $nResult = $objCompactTable->getCount( $strLocator ) ;
                if( $nResult > 0 )
                {
                    $strResult = '该合同类型设置在被使用中，无法删除。' ;
                    Cts_Json::echoResult( COMPACT_TYPE_IN_USED , $strResult ) ;
                }
                else
                {
                    $strLocator = 'id = '.$nTypeId ;
                    if( $objCompactTypeTable->delete( $strLocator ) )
                    {
                        $strResult = '删除合同类型成功。' ;
                        Cts_Json::echoResult( COMPACT_TYPE_DELETE_SUCCESSFUL , $strResult ) ;  
                        //write operation into db
        		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;     
        		    }
        		    else
        		    {
        		        $strResult = '删除合同类型失败。' ;
                        Cts_Json::echoResult( COMPACT_TYPE_DELETE_FAILD , $strResult ) ;  
                        //write operation into db
        		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ; 
        		    }
                }
            }
            else
            {
                $strResult = '提交的参数不合法，请重新选择要删除的信息。' ;
                Cts_Json::echoResult( COMPACT_TYPE_PARAMS_INVALIDED , $strResult ) ;  
                //write operation into db
		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }     
    }
}